#include <fcntl.h>


/*

When open is used under DOS 3.3, param3 is the file's type:

    Param3,hex    File type

    00            Text
    01            Integer basic
    02            Applesoft basic
    04            Binary
    08            Relocatable
    10            S-type file
    20            A-type file
    40            B-type file

*/

extern int textbase[24];

int dlodelo(name)
char *name;
{
   int fh,y,status=-2;
   int c, fa = 0, fl = 0;
   int height,packet,jdx,idx;
   char tempchar[2], name1[33], name2[33], D,L,O;


   jdx = 999;
   for (idx = 0; name[idx] != 0; idx++) {
        name1[idx] = name[idx];
        name2[idx] = name[idx];
		if (name[idx] == '.') jdx = idx;
   }
   name1[idx] = name2[idx] = 0;

   if (jdx != 999) {
	  D = toupper(name[jdx+1]);
	  L = toupper(name[jdx+2]);
	  O = toupper(name[jdx+3]);
	  if (D == 'D' && L == 'L' && (O == '1' || O == '2')) {
	       name1[jdx+3] = '1';
	       name2[jdx+3] = '2';
	  }
   }

   fh = open(name1,O_RDONLY,4); /* open a binary file */
   if (fh == -1)return -1;

   c = read(fh,&fa,2);
   if (c == 2)c = read(fh,&fl,2);

   switch(fl) {
	   case 1922:
	        /* is it a DLO ? */
			c = read(fh,tempchar,2);
			if (c!=2) break;
			packet= (int)tempchar[0];
			height= (int)tempchar[1];
			if (height != 24 || packet != 80)break;
			for(y=0;y<height;y++) {
				/* read each raster to the screen */
				/* switch to auxilliary memory - page2 */
				poke(49237,0);
				c = read(fh,(char *)textbase[y],40);
				/* switch back to main memory - page1 */
				poke(49236,0);
				if (c!=40)break;
				c = read(fh,(char *)textbase[y],40);
				if (c!=40)break;
			}
			if (c == 40)status = 0;
			break;

	   case 1016:
	       /* is it a bsaved image */
		   /* switch to auxilliary memory - page2 */
		   poke(49237,0);
	       c = read(fh,(char *)textbase[0],fl);
		   /* switch back to main memory - page1 */
		   poke(49236,0);
           if (c != 1016)break;
           close(fh);
           fh = open(name2,O_RDONLY,4); /* open a binary file */
           if (fh == -1)return -1;
           c = read(fh,&fa,2);
           if (c != 2)break;
           c = read(fh,&fl,2);
           if (c!= 2 || fl != 1016) break;
           c = read(fh,(char *)textbase[0],fl);
           if (c != 1016)break;
           status=0;
		   break;
   }

   close(fh);

   return status;
}

